﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ElectronicCommerce.Framework.TcpHelper.Server
{
    /// <summary>
    /// Message Client Store
    /// </summary>
    public class MessageClientStore
    {
        #region Fields

        private readonly List<ClientInfo> store = new List<ClientInfo>();

        private readonly object syncLock = new object();

        #endregion

        #region Helper

        public void AddClient(ClientInfo client)
        {
            lock (syncLock)
            {
                store.Add(client);
            }
        }

        public void RemoveClient(Guid clientId)
        {
            lock (syncLock)
            {
                if (ContainsClient(clientId))
                {
                    store.Remove(store.Find(c => c.Id == clientId));
                }
            }
        }

        public ClientInfo GetClientInfoById(Guid clientId)
        {
            return store.Find(c => c.Id == clientId);
        }

        public IList<ClientInfo> GetClientInfos()
        {
            return store.AsReadOnly();
        }

        public bool ContainsClient(Guid clientId)
        {
            return store.Count(c => c.Id == clientId) > 0;
        }

        public int ClientCount()
        {
            return store.Count();
        }

        #endregion
    }
}
